//
//  StudentsInfo.swift
//  StudentPerformance
//
//  Created by ap0stader on 2023/11/2.
//

import UIKit

class StudentsInfo {
    var studentsCollection = [Student]()
    
    init() {
        let nameOfStudents = [
            "Tommy",
            "Jerry",
            "Kate",
            "Ben",
            "Jimmy",
            "Ada",
            "Susan"
        ];
        
        let scoreOfStudents = [
            98,
            65,
            78,
            100,
            85,
            79,
            95,
            80
        ]
        
        let idOfStudent = [
            "37060101",
            "37060102",
            "37060103",
            "37060104",
            "37060105",
            "37060106",
            "37060107",
            "37060108"
        ];
        
        for i in 0...(nameOfStudents.count - 1) {
            let theStudent = Student(name: nameOfStudents[i], score: scoreOfStudents[i], id: idOfStudent[i])
            
            studentsCollection.append(theStudent)
        }
        
    }
    
    func addStudent() -> Student {
        let theStudent = Student(name: "New", score: 100, id: "20232023")
        studentsCollection.append(theStudent)
        return theStudent
    }
    
    func deleteStudent(_ theStudent: Student) {
        if let theIndex = studentsCollection.firstIndex(of: theStudent) {
            studentsCollection.remove(at: theIndex)
        }
    }
    
    func transferPosition(sourceIndex: Int, destinationIndex: Int) {
        let theStudent = studentsCollection[sourceIndex]
        studentsCollection.remove(at: sourceIndex)
        studentsCollection.insert(theStudent, at: destinationIndex)
    }
}
